/*
Uninclude: Tool for detecting unused #included files in C and C++ code.
    Copyright (C) 2012  Daniel Gutson, FuDePAN

    Uninclude is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version./

    Uninclude is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Uninclude.  If not, see <http://www.gnu.org/licenses/>.

    This is the class that determines whether a removed include is useful or not.
*/

#ifndef PROGRAMS_COMPARER_H
#define PROGRAMS_COMPARER_H

#include "uninclude/node_api.h"
#include "uninclude/compiler_driver.h"
#include "uninclude/bindiff_driver.h"
#include "uninclude/tmp_file.h"

class ProgramsComparer : public NodeAPI
{
    TmpFile originalBin;
    TmpFile tmpBin;
    const string tmpName;
    CompilerDriver& compiler;
    BinDiffDriver& diff;

    virtual bool programsIdentical()
    {
        if ( compiler.compile(tmpName, tmpBin.getName(), true ))
            return diff.binariesEqual( originalBin.getName(), tmpBin.getName() );
        else
            return false;
    }

    virtual bool checkIfIncludeFileCompiles(const string& hfile)
    {
        return compiler.compileHeader(hfile, true);
    }
public:
    ProgramsComparer(const string& originalName, const string& tmpName, CompilerDriver& compiler, BinDiffDriver& diff)
        : originalBin("bin_orig"), tmpBin("bin_tmp"), tmpName(tmpName), compiler(compiler), diff(diff)
    {
        if ( !compiler.compile(originalName, originalBin.getName(), false) )
            throw "Error compiling original program.";
    }
};

#endif

